﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using WebCrawler.Service;

namespace WebCrawler.ViewModel
{
    public class WebSingleViewModel : ViewModelBase
    {
        public WebSingleViewModel()
        {
            initBaseData();
        }

        #region 属性
        public string Url
        {
            get { return _url; }
            set { _url = value; RaisePropertyChanged(() => Url); }
        }
        private string _url;

        //  图片前缀
        public string Prefix
        {
            get { return _prefix; }
            set { _prefix = value; RaisePropertyChanged(() => Prefix); }
        }
        private string _prefix;

        // 全景图名
        public string Name
        {
            get { return _name; }
            set { _name = value; RaisePropertyChanged(() => Name); }
        }
        private string _name;

        public string StepMessage
        {
            get { return _stepMessage; }
            set { _stepMessage = value; RaisePropertyChanged(() => StepMessage); }
        }
        private string _stepMessage;

        public bool IsDownloading
        {
            get { return _isDownloading; }
            set { _isDownloading = value; RaisePropertyChanged(() => IsDownloading); }
        }
        private bool _isDownloading = false;
        #endregion

        #region 命令
        public RelayCommand StartDownloadCmd
        {
            get
            {
                if (_startDownloadCmd == null)
                    _startDownloadCmd = new RelayCommand(startDownload, canStartDownload);
                return _startDownloadCmd;
            }
            set { _startDownloadCmd = value; }
        }
        public RelayCommand _startDownloadCmd;
        #endregion

        #region 辅助函数
        private void initBaseData()
        {
            Url = "https://ssl-panoimg127.720static.com/resource/prod/ab936e32s6y/e8ajOztyuw0/29681382/imgs";
            Prefix = "mobile";
            Name = "山峡大坝";
        }

        private async void startDownload()
        {
            //string folderPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}/{Name}";
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}/{Name}";

            StepMessage = "开始";
            IsDownloading = true;

            Dictionary<string, Mat> mats = new Dictionary<string, Mat>();

            for (int i = 0; i < 6; i++)
            {
                string side = string.Empty;
                switch (i)
                {
                    case 0: side = "f"; break;
                    case 1: side = "b"; break;
                    case 2: side = "r"; break;
                    case 3: side = "l"; break;
                    case 4: side = "u"; break;
                    case 5: side = "d"; break;
                }

                StepMessage = $"下载：{Url}/{Prefix}_{side}.jpg";
                Mat mat = await ImageUtility.DownloadImageAsync($"{Url}/{Prefix}_{side}.jpg", $"{folderPath}/{side}.jpg");
                mats.Add(side, mat);
            }

            StepMessage = $"合并：Cubemap";
            Mat cubemap = await ImageUtility.MergeCubemapAsync(folderPath, mats);

            StepMessage = $"合并：Panorama";
            await ImageUtility.CubemapToPanoramaAsync(folderPath, cubemap);

            IsDownloading = false;
            StepMessage = "完成";
        }

        private bool canStartDownload()
        {
            return true;
        }
        #endregion

        public void ReleaseRegister()
        {
            Messenger.Default.Unregister(this);
        }
    }
}